//
//  VillageTableViewCell.m
//  Cunli
//
//  Created by 湛亚 on 16/1/22.
//  Copyright (c) 2016年 lilubin. All rights reserved.
//

#import "VillageTableViewCell.h"
#import "UIImageView+EMWebCache.h"
#import "PointsManager.h"
//#import "ConvertToCommonEmoticonsHelper.h"
//#import "MaxResponseButton.h"
#import "UIImageView+WebCache.h"
#import "UIColor+Addition.h"
#import "LinLi.h"
#import "TTTAttributedLabel.h"

@interface VillageTableViewCell ()<TTTAttributedLabelDelegate>
@end

@implementation VillageTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier andLinLi:(LinLi *)linLi andRange:(int)range{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _headImg=[[UIImageView alloc]init];
        _headImg.frame=CGRectMake(15, 13, 40, 40);
        _headImg.layer.cornerRadius = 20;
        _headImg.clipsToBounds = YES;
        _headImg.userInteractionEnabled=YES;
        UITapGestureRecognizer *headImgTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lookPersonInfo)];
        [_headImg addGestureRecognizer:headImgTap];
        if ([PointsManager isDefaultTouXiangWithUrlstring:linLi.txpic]) {
            _headImg.image=[UIImage imageNamed:@"default_touxiang.png"];
        }else{
            [_headImg setImageWithURL:[NSURL URLWithString:linLi.txpic] placeholderImage:[UIImage imageNamed:@"default_touxiang.png"]];
        }
        [self.contentView addSubview:_headImg];
        
        CGFloat nameLabelWidth = [self getNameLabelWidthWithString:linLi.truename]>ScreenWidth-130?ScreenWidth-130:[self getNameLabelWidthWithString:linLi.truename];
        _nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(65, 7, nameLabelWidth, 30)];
        _nameLabel.text=linLi.truename;
        _nameLabel.font=[UIFont systemFontOfSize:16];
        _nameLabel.userInteractionEnabled=YES;
        _nameLabel.textColor=[UIColor convertHexColorToUIColor:0x333333];
        UITapGestureRecognizer *nameTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lookPersonInfo)];
        [_nameLabel addGestureRecognizer:nameTap];
        [self.contentView addSubview:_nameLabel];
        
        if (linLi.type==1) {
            _helpLabel=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-55, 12, 40, 20)];
            _helpLabel.text=@"互助";
            _helpLabel.textAlignment=1;
            _helpLabel.textColor=[UIColor whiteColor];
            _helpLabel.font=[UIFont systemFontOfSize:14];
            _helpLabel.backgroundColor=[UIColor convertHexColorToUIColor:0x5fbafd];
            [self.contentView addSubview:_helpLabel];
        }
        
        UILabel *timeLab=[[UILabel alloc]initWithFrame:CGRectMake(65, 36, 120, 20)];
        timeLab.textColor=[UIColor convertHexColorToUIColor:0x999999];
        timeLab.text=linLi.addtime;
        timeLab.font=[UIFont systemFontOfSize:13];
        [self.contentView addSubview:timeLab];
        
        //NSString *contentText = [ConvertToCommonEmoticonsHelper convertToSystemEmoticons:linLi.introduce];
        //float height=[self heightWithString:contentText];
        float height=linLi.contentHeight;
        NSInteger lines;
        
        if (height>150) {
            height=150;
            lines=7;
            UILabel  *lookAllcontentLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, height+ 60, 50, 20)];
            lookAllcontentLabel.text=@"全文";
            lookAllcontentLabel.font=[UIFont systemFontOfSize:16];
            lookAllcontentLabel.userInteractionEnabled=YES;
            lookAllcontentLabel.textColor=[UIColor convertHexColorToUIColor:0x5fbafd];
            [self.contentView addSubview:lookAllcontentLabel];
        }else{
            lines=0;
        }
        _contentLabel=[PointsManager createWithframe:CGRectMake(15, 57, ScreenWidth-30, height) andString:linLi.introduce];
        _contentLabel.numberOfLines=lines;
        _contentLabel.delegate=self;
        [self.contentView addSubview:_contentLabel];
        if (height==150) {
            height=height+90;
        }else{
            height=height+60;
        }
        if (linLi.pics.count>0) {
            //显示图片
            if (linLi.pics.count==1) {
                
                CGSize size=[PointsManager onePicHeightWithWidth:linLi.first_pic_width andHeight:linLi.first_pic_height];
                
                UIImageView *picImg=[[UIImageView alloc]init];
                picImg.frame=CGRectMake(15, height,size.width,size.height);
                picImg.contentMode=UIViewContentModeScaleAspectFill;
                picImg.clipsToBounds=YES;
                NSString *urlString=[linLi.pics objectAtIndex:0];
                [picImg sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"phoneplace.png"]];
                picImg.tag=0;
                picImg.userInteractionEnabled=YES;
                UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkBigPic:)];
                [picImg addGestureRecognizer:singleTap];
                [self.contentView addSubview:picImg];
                height=height+size.height+10;
                
            }else{
                
                float imgWidth=ceilf((ScreenWidth-40)/3.0);
                int rowCount=linLi.pics.count==4?2:3;
                
                for (int i=0; i<linLi.pics.count; i++) {
                    UIImageView *picImg=[[UIImageView alloc]init];
                    picImg.frame=CGRectMake(15+i%rowCount*(5+imgWidth), height+i/rowCount*(imgWidth+5), imgWidth, imgWidth);
                    picImg.contentMode=UIViewContentModeScaleAspectFill;
                    picImg.clipsToBounds=YES;
                    NSString *urlString=[linLi.pics objectAtIndex:i];
                    [picImg sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"phoneplace.png"]];
                    picImg.tag=i;
                    picImg.userInteractionEnabled=YES;
                    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkBigPic:)];
                    [picImg addGestureRecognizer:singleTap];
                    [self.contentView addSubview:picImg];
                }
                height=height+(linLi.pics.count+2)/3*(imgWidth+5)+5;
            }
            
        }
        
        if (range!=1&&linLi.s_area.length>0) {
            
            UIImageView *fromImgView=[[UIImageView alloc]initWithFrame:CGRectMake(15, height,13, 13)];
            fromImgView.image=[UIImage imageNamed:@"fromplace"];
            [self.contentView addSubview:fromImgView];
            
            UILabel *fromLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, height-2, ScreenWidth-50, 15)];
            fromLabel.font=[UIFont systemFontOfSize:12];
            fromLabel.textColor=[UIColor convertHexColorToUIColor:0x999999];
            fromLabel.text=[NSString stringWithFormat:@"来自%@",linLi.s_area];
            [self.contentView addSubview:fromLabel];
            height=height+20;
        }
        
        UIView *sperView=[[UIView alloc]initWithFrame:CGRectMake(0, height,ScreenWidth, 1)];
        sperView.backgroundColor=[UIColor convertHexColorToUIColor:0xdcdddd];
        [self.contentView addSubview:sperView];
        
        UIView *sperView1=[[UIView alloc]initWithFrame:CGRectMake(ScreenWidth/2.0, height+8,1, 24)];
        sperView1.backgroundColor=[UIColor convertHexColorToUIColor:0xdcdddd];
        [self.contentView addSubview:sperView1];
        
        float gapWidth=(ScreenWidth/2.0-48)/2.0;
        _helpImgPic=[[UIImageView alloc]initWithFrame:CGRectMake(gapWidth, height+10, 20, 20)];
        _helpImgPic.image=[UIImage imageNamed:@"commentimg"];
        [self.contentView addSubview:_helpImgPic];
        
        _commentCountLabel=[[UILabel alloc]initWithFrame:CGRectMake(gapWidth+28, height+10, 30, 20)];
        _commentCountLabel.textColor=[UIColor grayColor];
        if (linLi.commentcount>0) {
            _commentCountLabel.text=[NSString stringWithFormat:@"%d",linLi.commentcount];
        }else{
            _commentCountLabel.text=@"评论";
        }
        _commentCountLabel.textColor=[UIColor convertHexColorToUIColor:0x999999];
        _commentCountLabel.font=[UIFont systemFontOfSize:15];
        [self.contentView addSubview:_commentCountLabel];
        
        _goodBtn = [[MaxResponseButton alloc] initWithFrame:CGRectMake( ScreenWidth/2.0+gapWidth,  height+10, 20, 20)];
        NSString *praiseImgName=linLi.is_prase==0?@"notpraise":@"addpraise";
        [_goodBtn setImage:[UIImage imageNamed:praiseImgName] forState:UIControlStateNormal];
        [_goodBtn addTarget:self action:@selector(goodBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_goodBtn];
        
        _zanCountLab=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2.0+gapWidth+28, height+10, 30, 20)];
        _zanCountLab.font=[UIFont systemFontOfSize:15];
        if (linLi.is_prase==0) {
            _zanCountLab.textColor=[UIColor convertHexColorToUIColor:0x999999];
        }else{
            _zanCountLab.textColor=[UIColor convertHexColorToUIColor:0xfb415c];
        }
        _zanCountLab.text=linLi.look>0?[NSString stringWithFormat:@"%d",linLi.look]:@"赞";
        [self.contentView addSubview:_zanCountLab];
        
        UIView *sperView2=[[UIView alloc]initWithFrame:CGRectMake(0, height+40,ScreenWidth, 15)];
        sperView2.backgroundColor=[UIColor  convertHexColorToUIColor:0xf6f6f6];
        [self.contentView addSubview:sperView2];
    }
    return self;
}

- (CGFloat)getNameLabelWidthWithString:(NSString *)nameString{
    
    CGSize resultSize = CGSizeZero;
    if (nameString.length <= 0) {
        return resultSize.width;
    }
    resultSize = [nameString boundingRectWithSize:CGSizeMake(ScreenWidth, 30)
                                          options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin)
                                       attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]}
                                          context:nil].size;
    resultSize = CGSizeMake(MIN(ScreenWidth, ceilf(resultSize.width)), MIN(30, ceilf(resultSize.height)));
    return resultSize.width;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

-(void)lookPersonInfo{
    
    _goToPersonInfo(_selectedIndexPath.row);
}

-(void)goodBtnAction{
    
    _addLike(_selectedIndexPath.row);
}

-(void)checkBigPic:(UIGestureRecognizer *)gestureRecognizer{
    
    _checkBigPics(_selectedIndexPath.row,gestureRecognizer.view.tag);
}

#pragma mark - TTTAttributedLabelDelegate 点击网址的超链接
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    
    NSLog(@">>>>>>%@",url);
    NSString *urlStr=[url absoluteString];
    NSRange foundObj=[urlStr rangeOfString:@"http://" options:NSCaseInsensitiveSearch];
    if(foundObj.length>0) {
        [[UIApplication sharedApplication] openURL:url];
    }else{
        
        urlStr=[NSString stringWithFormat:@"http://%@",urlStr];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
    }
    return;
}

-(void)reloadPraiseWithLinLi:(LinLi *)linli{
    
    if (linli.is_prase==0) {
        [_goodBtn setImage:[UIImage imageNamed:@"notpraise"] forState:UIControlStateNormal];
        _zanCountLab.textColor=[UIColor colorWithHexString:@"0x999999"];
    }else{
        [_goodBtn setImage:[UIImage imageNamed:@"addpraise"] forState:UIControlStateNormal];
        _zanCountLab.textColor=[UIColor colorWithHexString:@"0xfb415c"];
    }
    _zanCountLab.text=linli.look>0?[NSString stringWithFormat:@"%d",linli.look]:@"赞";
}

-(void)reloadCommentCountWithLinLi:(LinLi *)linli{
    
    if (linli.commentcount==0) {
        _commentCountLabel.text=@"评论";
    }else{
        _commentCountLabel.text=[NSString stringWithFormat:@"%d",linli.commentcount];
    }
}

@end
